package cn.qn.controller;

import cn.qn.dto.ResponseDTO;
import cn.qn.dto.sys.AreaDTO;
import cn.qn.dto.sys.CityDTO;
import cn.qn.dto.sys.ProvinceDTO;
import cn.qn.moudle.sys.Area;
import cn.qn.moudle.sys.City;
import cn.qn.moudle.sys.Province;
import cn.qn.service.sys.AreaService;
import cn.qn.service.sys.CityService;
import cn.qn.service.sys.ProvinceService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Bryant
 * @date: 2019/8/4
 * @description:
 */
@RestController
@RequestMapping("/province")
public class Test {

    @Reference(check = false)
    private ProvinceService provinceService;
    @Reference(check = false)
    private CityService cityService;
    @Reference(check = false)
    private AreaService areaService;


    @RequestMapping(value = "/json", name = "json", method = RequestMethod.GET)
    public ResponseDTO json() {
        List<Province> provinceList = provinceService.list();
        List<ProvinceDTO> pdtos=new ArrayList<>();
        provinceList.forEach(p->{
            ProvinceDTO pdto=new ProvinceDTO();
            pdto.setText(p.getName());
            pdto.setValue(p.getCode());
            List<CityDTO> cdtos=new ArrayList<>();
            List<City> citys=cityService.list(p.getCode());
            citys.forEach(c->{
                CityDTO cdto=new CityDTO();
                cdto.setText(c.getName());
                cdto.setValue(c.getCode());
                List<AreaDTO> adtos=new ArrayList<>();
                List<Area> areas=areaService.list(c.getCode());
                areas.forEach(a->{
                    AreaDTO adto=new AreaDTO();
                    adto.setText(a.getName());
                    adto.setValue(a.getCode());
                    adtos.add(adto);
                });
                cdto.setChildren(adtos);
                cdtos.add(cdto);
            });
            pdto.setChildren(cdtos);
            pdtos.add(pdto);
        });
        return ResponseDTO.success(pdtos);
    }
}
